package com.whq.thrift.api.web.admin.db.urlmapping;

import java.util.Map;

import org.apache.ibatis.jdbc.SqlBuilder;

import com.whq.common.StringUtil;

public class UrlMappingInfoSqlProvider {

	public String updateSql(UrlMappingInfo urlMappingInfo) {
		SqlBuilder.BEGIN();
		SqlBuilder.UPDATE("t_url_mapping");

		if (StringUtil.isNotBlank(urlMappingInfo.getMapParamMethod())) {
			SqlBuilder.SET("map_param_method = #{mapParamMethod}");
		}

		if (urlMappingInfo.getCreateTime() != null) {
			SqlBuilder.SET("create_time = #{createTime}");
		}

		if (urlMappingInfo.getUpdateTime() != null) {
			SqlBuilder.SET("update_time = #{updateTime}");
		}

		SqlBuilder.WHERE("url=#{url}");
		return SqlBuilder.SQL();
	}

	public String getSelectUrlMappingInfoSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("*");
		SqlBuilder.FROM("t_url_mapping");

		if ((parameter.get("url") != null) && (StringUtil.isNotBlank(parameter.get("url").toString()))) {
			SqlBuilder.WHERE("url like #{url}");
		}

		return SqlBuilder.SQL() + " limit #{beginIndex}, #{maxCount} ";
	}

	public String getSelectCountSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("count(*)");
		SqlBuilder.FROM("t_url_mapping");

		if ((parameter.get("url") != null) && (StringUtil.isNotBlank(parameter.get("url").toString()))) {
			SqlBuilder.WHERE("url like #{url}");
		}

		return SqlBuilder.SQL();
	}

	public String findUrlMappingApiList(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("*");
		SqlBuilder.FROM("t_url_mapping");

		if ((parameter.get("url") != null) && (StringUtil.isNotBlank(parameter.get("url").toString()))) {
			SqlBuilder.WHERE("url like #{url}");
		}
		
		if ((parameter.get("mapParamMethod") != null) && (StringUtil.isNotBlank(parameter.get("mapParamMethod").toString()))) {
			SqlBuilder.WHERE("map_param_method like #{mapParamMethod}");
		}
		
		return SqlBuilder.SQL();
	}

}
